<?php
class log_admin
{
	private $root_path;
	public $pages;
	
	function log_admin()
	{
		$this->root_path = LOG_PATH;
	}
	
	/**
	 * 获得已有日志类型
	 *
	 * @return array
	 */
	function get_log_type()
	{
		$types = array();
		if (is_dir($this->root_path)) 
		{
		    if ($dh = opendir($this->root_path)) 
		    {
		        while (($file = readdir($dh)) !== false) 
		        {
		        	if($file !== '.' && $file !== '..')
		        	{
		        		$types[] = $file;
		        	}
		        }
		        closedir($dh);
		    }
		}
		return $types;
	}
	
	/**
	 * 获得某一天的日志
	 *
	 * @param int $page
	 * @param int $pagesize
	 * @param varchar $type
	 * @param varchar $date
	 * @return array
	 */
	function get_logs_by_date($page,$pagesize,$type,$date=false)
	{
		$start = ($page - 1) * $pagesize;
		if($date === false)
		{
			$date = date('Y/m/d');
		}
		else 
		{
			$date = date('Y/m/d', strtotime($date));
		}
		$dir = $this->root_path . $type . '/' . $date . '.log';
		if(!file_exists($dir) || !is_file($dir))
		{
			return false;
		}
		
		$logs = file($dir);
		$list = array();
		if(is_array($logs) && sizeof($logs) > 0)
		{
			$i=0;
			foreach ($logs as $log)
			{
				$tmp = explode("|||", trim($log));
				if(sizeof($tmp) >= 4)
				{
					if($i>=$start && $i<($start + $pagesize))
					{
						
						$list[] = $tmp;
					}
					$i++;
				}
				if($i >= ($start + $pagesize))
				{
					break;
				}
			}
			$this->pages = pages(sizeof($logs),$page,$pagesize);
			$logs = array();
			return $list;
		}
		else 
		{
			return false;
		}
	}
	
	
	/**
	 * 删除指定时间范围内的日志
	 * 
	 * @param varchar $type
	 * @param varchar $time
	 * @return bool
	 */
	function del_logs($type,$time,$date=false)
	{
		$dir = $this->root_path . $type .'/';
		$dw = preg_replace('/[0-9]*/','',$time);
		$number = intval($time);
		if($dw=='w')
		{
			$dw = 'd';
			$number = $number * 7;
		}
		$start = $number - 1;
		switch ($dw)
		{
			case 'd':
				for ($i=$start; $i>=0; $i--)
				{
					$date = date('Y/m/d', mktime(0,0,0,date('m'),intval(date('d'))-$i,date('Y')));
					$this->delete_dir_files($dir . $date . '.log');
				}
				break;
			case 'm':
				for ($i=$start; $i>=0; $i--)
				{
					$date = date('Y/m/', mktime(0,0,0,intval(date('m'))-$i,1,date('Y')));
					$this->delete_dir_files($dir . $date);
				}
				break;
			case 'y':
				for ($i=$start; $i>=0; $i--)
				{
					$date = date('Y/', mktime(0,0,0,1,1,intval(date('Y'))-$i));
					$this->delete_dir_files($dir . $date);
				}
				break;
			case 'all':
				$this->delete_dir_files($dir);
				break;
			case 'current':
				if($date===false)
				{
					return false;
				}
				$date = date('Y/m/d',strtotime($date)) . '.log';
				$this->delete_dir_files($dir . $date);
				break;
			default:
				return false;
		}
		return true;
	}
	
	/**
	 * 删除文件或递归的删除目录及目录下的文件及子目录
	 *
	 * @param varchar $dir
	 * @return bool
	 */
	function delete_dir_files($dir)
	{
		if(is_dir($dir) && !is_file($dir))
		{
			if ($dh = opendir($dir)) 
		    {
		        while (($file = readdir($dh)) !== false) 
		        {
		        	if($file !== '.' && $file !== '..')
		        	{
		        		$this->delete_dir_files($dir.'/'.$file);
		        	}
		        }
		        closedir($dh);
		        if(sizeof(scandir($dir)) == 2)
		        {
		        	rmdir($dir);
		        }
		        return true;
		    }
		}
		else 
		{
			if(is_file($dir))
			{
				unlink($dir);
			}
			return true;
		}
	}
}
?>